package org.codegallery.javagal.grizzly;

import java.io.IOException;
import java.util.Enumeration;

import javax.management.ObjectName;

import com.sun.grizzly.SSLConfig;
import com.sun.grizzly.http.Management;
import com.sun.grizzly.http.embed.GrizzlyWebServer;
import com.sun.grizzly.http.servlet.ServletAdapter;
import com.sun.grizzly.tcp.http11.GrizzlyAdapter;
import com.sun.grizzly.tcp.http11.GrizzlyRequest;
import com.sun.grizzly.tcp.http11.GrizzlyResponse;


public class SyncEmbeddWebServerTest {
	public static final String ROOT="D:\\wamp\\www";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		syncCustomizedResource();

	}

	private static void syncStaticResource() {
		GrizzlyWebServer ws = new GrizzlyWebServer(ROOT);
		try {
			ws.start();
		} catch (IOException ex) {
			// Something when wrong.
		}
	}

	private static void syncCustomizedResource() {
		GrizzlyWebServer ws = new GrizzlyWebServer(ROOT);
		try {
			ws.addGrizzlyAdapter(new GrizzlyAdapter() {

				public void service(GrizzlyRequest request,
						GrizzlyResponse response) {
					try {
						System.out.println("-------head properties-------------");
						Enumeration enu=request.getHeaderNames();
						while(enu.hasMoreElements()){
							String header=(String)enu.nextElement();
							System.out.println(header+ ": "+request.getHeader(header));
						}
						System.out.println("---------head properties-----------");
						response.getWriter().println("Grizzly is soon cool");
					} catch (IOException ex) {
					}
				}
			});
			ws.start();
		} catch (IOException ex) {
			// Something when wrong.
		}
	}

	private static void syncServlet() {
		GrizzlyWebServer ws = new GrizzlyWebServer(ROOT);
		try {
			ServletAdapter sa = new ServletAdapter();
			sa.setRootFolder("/Path/To/Exploded/War/File");
			sa.setServletInstance(new MyServlet());
			ws.addGrizzlyAdapter(sa);
			ws.start();
		} catch (IOException ex) {
			// Something when wrong.
		}
	}

	private static void syncTwoServlets() {
		GrizzlyWebServer ws = new GrizzlyWebServer(ROOT);
		try {
			ServletAdapter sa = new ServletAdapter();
			sa.setRootFolder("/Path/To/Exploded/War/File");
			sa.setServletInstance(new MyServlet());
			ws.addGrizzlyAdapter(sa);

			ServletAdapter sa2 = new ServletAdapter();
			sa2.setRootFolder("/Path/To/Exploded/War2/File");
			sa2.setServletInstance(new MySecondServlet());
			ws.addGrizzlyAdapter(sa2);

			ws.start();
		} catch (IOException ex) {
			// Something when wrong.
		}
	}

	private static void syncStaticResourceJMX() {
		try {
			GrizzlyWebServer ws = new GrizzlyWebServer(ROOT);
			// “-Djavax.management.builder.initial=mx4j.server.MX4JMBeanServerBuilder"
			ws.enableJMX(new Management() {

				public void registerComponent(Object bean, ObjectName oname,
						String type) throws Exception {
					// Registry.getRegistry().registerComponent(bean,oname,type);
				}

				public void unregisterComponent(ObjectName oname)
						throws Exception {
					// Registry.getRegistry().
					unregisterComponent(oname);
				}
			});

			ws.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void syncSSL() {
		try {
			GrizzlyWebServer ws = new GrizzlyWebServer(443, ROOT, true);
			SSLConfig sslConfig = new SSLConfig();
			sslConfig.setTrustStoreFile("Path-to-trustore");
			sslConfig.setKeyStoreFile("Path-to-Trustore");
			ws.setSSLConfig(sslConfig);
			System.out.println("Grizzly WebServer listening on port 8080");
			ws.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
